<template>
	<div>
		<van-nav-bar
		  title="新增收货地址"
			left-text="返回"
			 @click-left="handleLeftClick"
		/>
		<van-address-edit
		  :area-list="areaList"
		  show-postal
		  show-set-default
		  show-search-result
		  :area-columns-placeholder="['请选择', '请选择', '请选择']"
			@change-area="handleAreaChange"
		  @save="onSave"
		  @change-detail="onChangeDetail"
		/>
	</div>
</template>

<script>
	import {areaList} from '@/data/areaList.js'
	import {insertAddress} from '@/api/address-api.js'
	export default{
		data(){
			return {
				areaList:areaList,
				formData:{}
			}
		},
		methods:{
			handleLeftClick(){
				this.$router.history.go(-1)
			},
			async onSave(formData){
				
				this.formData = {
					
					...this.formData,
					...formData,
					phone:formData.tel,
					postCode:formData.postalCode,
					isDefault:formData.isDefault?1:0,
					address:formData.addressDetail
				}
				console.log(this.formData)
				let res = await insertAddress(this.formData)
				if(res.data.code ==200){
					this.$router.history.go(-1)
				}
			},
			onChangeDetail(){
				
			},
			handleAreaChange(res){
				console.log(res)
				if(res.length>0){
					this.formData.provinceId = res[0].code
					this.formData.province = res[0].name
					this.formData.cityId = res[1].code
					this.formData.city = res[1].name
					this.formData.areaId = res[2].code
					this.formData.area = res[2].name
				}
			}
		}
	}
</script>

<style>
</style>
